package cn.itcast.bos.action.system;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.UserService;
import cn.itcast.bos.utils.BaseAction;

public class UserAction extends BaseAction<User> {
	@Autowired
	private UserService userService;
	@Action(value="user_login",results={
			@Result(name="success",type="redirect",location="index.html"),
			@Result(name="login",type="redirect",location="login.html")})
	public String login(){
		Subject subject = SecurityUtils.getSubject();
		AuthenticationToken token = new UsernamePasswordToken(model.getUsername(),model.getPassword());
		try {
			//登陆成功
			subject.login(token);
			return SUCCESS;
		} catch (AuthenticationException e) {
			//登陆失败
			e.printStackTrace();
			return LOGIN;
		}
	}
	@Action(value="user_logout",results={@Result(name="success",type="redirect",location="login.html")})
	public String logout(){
		SecurityUtils.getSubject().logout();
		return SUCCESS;
	}
}
